package com.bj58.migrate;

import java.util.List;

import com.bj58.migrate.helper.Helper;
import com.bj58.migrate.model.IPreparedStatementHandlerImpl;
import com.bj58.sfft.cmc.entity.Category;
import com.bj58.sfft.cmc.entity.DispCategory;
import com.bj58.sfft.utility.dao.basedao.DAOBase;
import com.bj58.sfft.utility.dao.basedao.DAOHelper;
import com.bj58.sfft.utility.dao.basedao.IPreparedStatementHandler;

public class CatetoryDMigrate{

	static final IPreparedStatementHandler handler = new IPreparedStatementHandlerImpl();

	static DAOHelper daoHelpers = null;

	static{
		try{
			String configPath = Helper.seekFile( "db0.properties" ).getAbsolutePath();
			daoHelpers = DAOBase.createIntrance( configPath );
		}
		catch( Exception e ){
			e.printStackTrace();
		}
	}

	public void migrate(){

		try{
			List< DispCategory > list = ScfProxy.cmc_DispCategory().GetDispCategoryByParentID( 9224 );
			if( list != null ){
				for( DispCategory dispCategory : list ){
					if( dispCategory != null && dispCategory.getIsVisible() ){
						insertDB( dispCategory );
						if( dispCategory.getDepth() == 1 && dispCategory.getIsVisible() && dispCategory.getType() != 2 ){
							List< DispCategory > children = ScfProxy.cmc_DispCategory().GetDispCategoryByParentID( dispCategory.getDispCategoryID() );
							if( children != null ){
								for( DispCategory child : children ){
									if( child != null && child.getIsVisible() && child.getType() != 2 ){
										insertDB( child );
									}
								}
							}
						}
					}
				}
			}
		}
		catch( Exception e ){
			e.printStackTrace();
		}
	}

	private void insertDB( DispCategory dispCategory ){

		try{

			// daoHelpers.execInsert(
			// "insert into t_zp_d_category( id,pid,catename,depth,listname,level0,level1,level2,white ) values( 574,0,'招聘',0,'job',574,0,0,0)",
			// handler );

			StringBuilder sql =
					new StringBuilder( "insert into t_zp_d_category( id,pid,catename,depth,listname,level0,level1,level2,white ) values( " );
			Category catetory = ScfProxy.cmc_Category().GetCategoryByID( Integer.parseInt( dispCategory.getInfoCateidList() ) );
			sql.append( dispCategory.getInfoCateidList() ).append( "," );
			sql.append( catetory.getPID() ).append( ",'" );
			sql.append( dispCategory.getCateName() ).append( "'," );
			sql.append( dispCategory.getDepth() ).append( ",'" );
			sql.append( dispCategory.getListName() ).append( "'," );

			String[] array = catetory.getFullPath().split( "," );
			sql.append( array[ 0 ] ).append( "," );

			if( array.length == 1 ){
				sql.append( 0 ).append( "," );
				sql.append( 0 ).append( "," );
			}
			else if( array.length == 2 ){
				sql.append( array[ 1 ] ).append( "," );
				sql.append( 0 ).append( "," );
			}
			else if( array.length == 3 ){
				sql.append( array[ 1 ] ).append( "," );
				sql.append( array[ 2 ] ).append( "," );
			}

			sql.append( 0 );
			sql.append( ")" );

			System.out.println( sql.toString() );
			daoHelpers.execInsert( sql.toString(), handler );
		}
		catch( Exception e ){
			e.printStackTrace();
		}
	}
}
